﻿package mortal.game.view.copy.petBreak
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import extend.language.*;
    import mortal.component.window.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.ChallengeCopy.view.*;
    import mortal.game.view.copy.petBreak.vo.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class PetBreakCopyController extends CopyControllerBase
    {
        private var _moduel:PetBreakCopyModule;
        private var _isEnter:Boolean;
        private var _petPlayer:MovePlayer;

        public function PetBreakCopyController()
        {
            return;
        }// end function

        private function onInfoGet(event:DataEvent) : void
        {
            return;
        }// end function

        private function onBuyHP(event:DataEvent) : void
        {
            return;
        }// end function

        private function onPointItemOper(event:DataEvent) : void
        {
            if (cache.pet.pets.length <= 0)
            {
                MsgManager.showRollTipsMsg(Language.getString(70344));
                return;
            }
            var _loc_2:* = event.data as PetBreakCopyOperVo;
            if (_loc_2.type == PetBreakCopyUtil.ENTER)
            {
                GameProxy.copy.enterPetBreakCopy(_loc_2);
            }
            return;
        }// end function

        private function vipLevelUp(param1:Object) : void
        {
            this._moduel.setSweepTxt();
            return;
        }// end function

        private function onOpenByIndex(event:DataEvent) : void
        {
            view.show();
            var _loc_2:* = event.data ? (event.data as PetBreakCopyOpenData) : (new PetBreakCopyOpenData());
            this._moduel.openByIndex(_loc_2);
            return;
        }// end function

        private function onCopyEndInfo(param1:Object) : void
        {
            var _loc_2:* = param1.result as SPetBreakCopyResult;
            var _loc_3:* = param1.isSweep;
            PetBreakCopyResultView.instance.show();
            PetBreakCopyResultView.instance.setData(_loc_2, _loc_3);
            if (ChallengeTimePanel.isViewShow)
            {
                ChallengeTimePanel.instance.stopCount();
            }
            return;
        }// end function

        private function onCopyTime(param1:Object) : void
        {
            var _loc_2:* = param1 as SPetBreakCopyTime;
            ChallengeTimePanel.instance.show();
            ChallengeTimePanel.instance.data = PetBreakCopyUtil.getChallengeTimeData(_loc_2.floor, _loc_2.level);
            ChallengeTimePanel.instance.startCount = _loc_2.leftTime;
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            this._isEnter = true;
            super.enterCopy(param1);
            AIManager.cancelAll();
            Game.scene.removeRole();
            this.setViewTarget();
            this.view.hide();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupAvatarVisible, false));
            Dispatcher.dispatchEvent(new DataEvent(EventName.AvatarModuleVisible, false));
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetBreakCopyPetAvatarAdjust, true));
            return;
        }// end function

        override public function leaveCopy() : void
        {
            this._petPlayer = null;
            this._isEnter = false;
            super.leaveCopy();
            AIManager.cancelAll();
            Game.scene.addRole();
            Game.scene.viewTarget = null;
            ChallengeTimePanel.instance.hide();
            Dispatcher.dispatchEvent(new DataEvent(EventName.SkillExCDRemoveAll));
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupAvatarVisible, true));
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutBarShowHide, true));
            Dispatcher.dispatchEvent(new DataEvent(EventName.AvatarModuleVisible, true));
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetBreakCopyPetAvatarAdjust, false));
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetBreakCopySceneEntityPetUpdate, false));
            return;
        }// end function

        private function addPet(event:DataEvent) : void
        {
            this._petPlayer = event.data as MovePlayer;
            this.setViewTarget();
            return;
        }// end function

        private function setViewTarget() : void
        {
            if (this._isEnter && this._petPlayer)
            {
                Game.scene.viewTarget = this._petPlayer;
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetBreakCopySceneEntityPetUpdate, true));
            }
            return;
        }// end function

        private function onSceneAddSelfPet(event:DataEvent) : void
        {
            var _loc_2:* = event.data as EntityInfo;
            cache.copy.petBreakCopy.entityInfo = _loc_2.entityInfo;
            return;
        }// end function

        private function onIconShow(param1) : void
        {
            var _loc_2:* = CopyConfig.instance.getCopyInfoByCode(113);
            if (cache.role.entityInfo.level >= _loc_2.enterMinLevel)
            {
                PetBreakCopyIcon.instance.show();
            }
            return;
        }// end function

        private function onShow(event:WindowEvent) : void
        {
            Dispatcher.addEventListener(EventName.PetBreakCopyInfoGet, this.onInfoGet);
            Dispatcher.addEventListener(EventName.PetBreakCopyBuyHP, this.onBuyHP);
            Dispatcher.addEventListener(EventName.PetBreakCopyPointItemOper, this.onPointItemOper);
            NetDispatcher.addCmdListener(ServerCommand.VipLevelUp, this.vipLevelUp);
            return;
        }// end function

        private function onClose(event:WindowEvent) : void
        {
            Dispatcher.removeEventListener(EventName.PetBreakCopyInfoGet, this.onInfoGet);
            Dispatcher.removeEventListener(EventName.PetBreakCopyBuyHP, this.onBuyHP);
            Dispatcher.removeEventListener(EventName.PetBreakCopyPointItemOper, this.onPointItemOper);
            NetDispatcher.removeCmdListener(ServerCommand.VipLevelUp, this.vipLevelUp);
            return;
        }// end function

        override protected function addListeners() : void
        {
            Dispatcher.addEventListener(EventName.Scene_AddEntityPet, this.addPet);
            return;
        }// end function

        override protected function removeListeners() : void
        {
            Dispatcher.removeEventListener(EventName.Scene_AddEntityPet, this.addPet);
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.PetBreakCopyOpenByIndex, this.onOpenByIndex);
            Dispatcher.addEventListener(EventName.SceneAddSelfPet, this.onSceneAddSelfPet);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.onIconShow);
            NetDispatcher.addCmdListener(ServerCommand.PetBreakCopyEndInfo, this.onCopyEndInfo);
            NetDispatcher.addCmdListener(ServerCommand.PetBreakCopyTime, this.onCopyTime);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onIconShow);
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._moduel == null)
            {
                this._moduel = new PetBreakCopyModule();
                this._moduel.addEventListener(WindowEvent.SHOW, this.onShow);
                this._moduel.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._moduel;
        }// end function

    }
}
